package model
{
	import com.StatusBar;
	
	import flash.net.URLRequestMethod;
	
	import mx.rpc.events.FaultEvent;
	
	import utils.HttpServiceHelper;
	import utils.Utilities;
	
	public class ModelRequest
	{
		public static const GET_REPORTS_CATALOG:String = 'C';	//report catalog - list of reports
		public static const GET_REPORT_VARIABLES:String = 'V';	//single report's variables
		public static const GET_VARIABLE_F4:String = 'F';		//F4 values for a single variable
		public static const GET_REPORT:String = 'R';			//get a report data with header info
		public static const GET_REPORT_HEADER_ONLY:String = 'H';			//get a report header ONLY
		public static const GET_MORE_REPORT_DATA:String = 'L';	//get more of same report data - lazy loader
		public static const GET_CONFIG:String = 'G';			//load config xml
		public static const GET_AUTH_CHECK:String = 'A';			//user auth check
		public static const GET_EMP_INFO:String = 'E';			//get employee info
		public static const GET_FAVORITE_LIST:String = 'FG';			//get favorite reports list
		public static const SET_FAVORITE:String = 'FS';			//set favorite report
		public static const SET_CONFIG:String = 'GS';			//set config file
		public static const SEND_EMAIL:String = 'SE';			//send report as email
		public static const DOWNLOAD_EXCEL:String = 'DE';			//send report as html table and download it as excel
		public static const PRINT_PDF:String = 'PP';			//send report as html table and download it as excel
		
		public function ModelRequest()
		{
		}
		
		/**
		 * flag indicating completion status of this request.
		 * @return returns request url.
		 * */
		public function get isCompleted():Boolean
		{
			return _isCompleted;
		} 
		public function set isCompleted(value:Boolean):void
		{
			_isCompleted = value;
		}
		private var _isCompleted:Boolean = false;
		
		/**
		 * URL that will be used to send a request.
		 * @return returns request url.
		 * */
		public function get url():String
		{
			return _url;
		} 
		public function set url(value:String):void
		{
			_url = value;
		}
		private var _url:String;
		
		/**
		 * HTTP request method.
		 * */
		public function get httpRequestMethod():String
		{
			return _httpRequestMethod;
		} 
		public function set httpRequestMethod(value:String):void
		{
			_httpRequestMethod = value;
		}
		private var _httpRequestMethod:String = URLRequestMethod.GET;
		
		/**
		 * A flag for a request statue if sent or not.
		 * @return returns request type.
		 * */
		public function get isSent():Boolean
		{
			return _isSent;
		} 
		public function set isSent(value:Boolean):void
		{
			_isSent = value;
		}
		private var _isSent:Boolean = false;
		
		/**
		 * Available type: 'C' report catalog, 'V' vars, 'F' F4 values, 'R' report.
		 * @return returns request type.
		 * */
		public function get type():String
		{
			return _type;
		} 
		public function set type(value:String):void
		{
			_type = value;
		}
		private var _type:String;
		
		/**
		 * request parameters.
		 * @return returns request parameters.
		 * */
		public function get parameters():Object
		{
			return _parameters;
		} 
		public function set parameters(value:Object):void
		{
			_parameters = value;
		}
		private var _parameters:Object = new Object();
		
		/**
		 * result.
		 * @return returns result.
		 * */
		public function get result():ModelResult
		{
			return _result;
		} 
		public function set result(value:ModelResult):void
		{
			_result = value;
			_result.updateRequest = updateRequest;
		}
		private var _result:ModelResult;
		
		/**
		 * DataModel function to update queue of requests.
		 * @return returns result.
		 * */
		public function get updateQueue():Function
		{
			return _updateQueue;
		} 
		public function set updateQueue(value:Function):void
		{
			_updateQueue = value;
		}
		private var _updateQueue:Function;
		
		/**
		 * special case for lazy loader where need to pass opened node to attach data to it.
		 * */
		public function get LazyLoaderNode():Object
		{
			return _LazyLoaderNode;
		} 
		public function set LazyLoaderNode(value:Object):void
		{
			_LazyLoaderNode = value;
		}
		private var _LazyLoaderNode:Object;
		
		/**
		 * Call back function which is called after finishing a request. This function call will pass a result object to the requesting component.
		 * @return a Function reference which resides in requesting component.
		 * */
		public function get callBack():Function
		{
			return _callBack;
		} 
		public function set callBack(value:Function):void
		{
			_callBack = value;
		}
		private var _callBack:Function;
		
		public function send():void
		{
			HttpServiceHelper.invokeHttpService(url, result.resultHandler, faultHandler, parameters, null, httpRequestMethod);
			isSent = true;
			Utilities.setStatusMessage('Request is Sent.', StatusBar.NOTIFICATION);
		}
		
		private function updateRequest():void
		{
			isCompleted = true;
			updateQueue();
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			//update flags
			//result.isSuccessful = false;
			//result.isCompleted = true;
			isCompleted = true;
			//set status
			//Utilities.setStatusMessage('HTTP Request Error. Respond is not proper.', StatusBar.ERROR);
			//notify data model
			updateQueue();
		}
		
	}
}